package edu.chinasoft.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.annotation.WebServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import edu.chinasoft.domain.Function;
import edu.chinasoft.domain.PageUnitForRole;
import edu.chinasoft.domain.Role;
import edu.chinasoft.service.RoleService;

/**
 *
 * @author mhao
 * @date 2020年2月12日:下午3:56:48
 * @version V1.0
 * @Description: 
*/
@WebServlet(name = "myShowRoleServlet", value = "/servlet/showroleservlet")
public class ShowRoleServlet extends HttpServlet {
	private static Logger logger = LoggerFactory.getLogger(ShowRoleServlet.class);
	private RoleService roleService;
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" ShowRoleServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		//获取分页单位
		ServletContext servletContext = this.getServletContext();
		String initParameter = servletContext.getInitParameter("pageUnit");
		int pageUnit = Integer.parseInt(initParameter);
		//获取当前页
		String pageParameter = request.getParameter("currentPage");
		pageParameter=pageParameter==null?"1":pageParameter;
		int currentPage = Integer.parseInt(pageParameter);
		Map<String, Object> map = new HashMap<>();
		int setLimit=(currentPage-1)*pageUnit;
		map.put("setLimit", setLimit);
		map.put("pageUnit", pageUnit);
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		RoleService roleServiceImp = wac.getBean("roleService", RoleService.class);
		this.roleService=roleServiceImp;
		//调用服务层方法
		PageUnitForRole pageUnitForRole = roleService.getRecordNum();
		List<Role> roleList = roleService.selectUnitRole(map);
		for(Role role:roleList) {
			List<Function> functionList = roleService.selectFunctionByRoleId(role);
			role.setFunctionList(functionList);
		}
		pageUnitForRole.setRoleList(roleList);
		pageUnitForRole.setPageUnit(pageUnit);
		pageUnitForRole.setCurrentPage(currentPage);
		//加到session域里
		HttpSession session = request.getSession();
		session.setAttribute("rolePage", pageUnitForRole);
		response.sendRedirect(""+request.getContextPath()+"/showRole.jsp");
		logger.info(" ShowRoleServlet >doGet end.......");
	}

	/**
	 *
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" ShowRoleServlet >doPost start.......");
		this.doGet(request, response);
		logger.info(" ShowRoleServlet >doPost end.......");
	}

}